package org.vectrics.common.view;
/*
 * @author Mike Moore
 */

import org.vectrics.common.html.*;
import org.apache.ecs.Element;
import org.apache.log4j.Logger;

public class FieldTable extends Table {

	private static final long serialVersionUID = -8573671393470764270L;
	
	private final Logger log = Logger.getLogger(this.getClass());
    private String valueCellWidth = "60%";
    private String labelCellClass = null;
    private String valueCellClass = "fieldTableBg";
    private String labelFontClass = null;
    private String valueFontClass = "subFont";
    private boolean blankRowBefore = false;
	private String titleCellWidth = "38%";

    public FieldTable() {
        this.setCellPadding(3);
        this.setWidth("100%");
    }

    public void setLabelCellClass(String className) {
        labelCellClass = className;
    }

    public void setValueCellClass(String className) {
        valueCellClass = className;
    }

    public void setLabelFontClass(String className) {
        labelFontClass = className;
    }

    public void setValueFontClass(String className) {
        valueFontClass = className;
    }

    public void blankRowBeforeRows(boolean blankRowBefore) {
        this.blankRowBefore = blankRowBefore;
    }

    public Row addBlankRow(Table table) {
        Row fieldRow = table.addRow();
        Cell cell = fieldRow.addCell();
        cell.setColSpan(2);
        cell.addElement("&nbsp;");
        return(fieldRow);
    }
/*
    public Row addFieldRow(Table table, String label, String value) {
        if (blankRowBefore) {
            addBlankRow(table);
        }

        Row fieldRow = table.addRow();
        Cell labelCell = fieldRow.addCell();
        if (labelCellClass != null) {
            labelCell.setClass(labelCellClass);
        }
        Cell valueCell = fieldRow.addCell();
        if (valueCellClass != null) {
            valueCell.setClass(valueCellClass);
        }
        labelCell.addElement(TextHelper.getFont(labelFontClass, label));
        valueCell.addElement(TextHelper.getFont(valueFontClass, value));
        return(fieldRow);
    }

   */ 
    public void setValueCellWidth(String width) {
        valueCellWidth = width;
    }

    public Row showField(String title, String value) {
        Row row = this.addRow();
        Cell cellA = row.addCell();
        cellA.addElement(TextHelper.getFont("subFont", title));
        cellA.setWidth(titleCellWidth );
        Cell cellSep = row.addCell();
        cellSep.addElement("&nbsp;&nbsp;");
        cellSep.setWidth(1);
        Cell cellB = row.addCell();

        Table valueTable = new Table();
        valueTable.setWidth(valueCellWidth);
        cellB.addElement(valueTable);
        Cell valueCell = valueTable.addRow().addCell();
        if (value != null)
            valueCell.addElement(TextHelper.getFont(this.valueFontClass, value));
        if ((value == null) || (value.trim().length() <= 0)) {
            log.debug("force space to hold box height to non-zero because no text was put in box for value.");
            valueCell.addElement(TextHelper.getFont(this.valueFontClass, "&nbsp;"));
        }
        valueCell.setClass(valueCellClass);
        return(row);
    }

    public Row showFieldElement(String title, Element element) {
        Row row = this.addRow();
        Cell cellA = row.addCell();
        cellA.addElement(TextHelper.getFont("subFont", title));
        Cell cellSep = row.addCell();
        cellSep.addElement("&nbsp;&nbsp;");
        Cell cellB = row.addCell();
        
        Table valueTable = new BorderTable();
        valueTable.setWidth(valueCellWidth);
        cellB.addElement(valueTable);
        Cell valueCell = valueTable.addRow().addCell();
        valueCell.setClass(valueCellClass);
        valueCell.addElement(element);
        return(row);
    }

	/**
	 * @return the blankRowBefore
	 */
	public boolean isBlankRowBefore() {
		return blankRowBefore;
	}

	/**
	 * @param blankRowBefore the blankRowBefore to set
	 */
	public void setBlankRowBefore(boolean blankRowBefore) {
		this.blankRowBefore = blankRowBefore;
	}

	/**
	 * @return the titleCellWidth
	 */
	public String getTitleCellWidth() {
		return titleCellWidth;
	}

	/**
	 * @param titleCellWidth the titleCellWidth to set
	 */
	public void setTitleCellWidth(String titleCellWidth) {
		this.titleCellWidth = titleCellWidth;
	}

	/**
	 * @return the valueCellWidth
	 */
	public String getValueCellWidth() {
		return valueCellWidth;
	}

	/**
	 * @return the labelCellClass
	 */
	public String getLabelCellClass() {
		return labelCellClass;
	}

	/**
	 * @return the valueCellClass
	 */
	public String getValueCellClass() {
		return valueCellClass;
	}

	/**
	 * @return the labelFontClass
	 */
	public String getLabelFontClass() {
		return labelFontClass;
	}

	/**
	 * @return the valueFontClass
	 */
	public String getValueFontClass() {
		return valueFontClass;
	}
     
}